<?php

class VNExpress {

    var $links = array();
    var $title = "";
    var $intro = "";
    var $content = "";
    var $imgurl = "";
    var $imglist = array();

    function Init() {
        $this->title = "";
        $this->intro = "";
        $this->content = "";
        $this->imgurl = "";
    }

    function GetLink($link) {
        $link = trim($link);

        if (getFileExt($link) == "rss") {
            $document = trim(FetchDataString($link));
            preg_match_all('/<a href="(.*?)">/s', $document, $result, PREG_PATTERN_ORDER);
            preg_match_all('/<img src="(.*?)">/i', $document, $imgs, PREG_PATTERN_ORDER);

            for ($i = 0; $i < sizeof($result[1]); $i++) {
                array_push($this->links, $result[1][$i]);
                array_push($this->imglist, $imgs[1][$i]);
            }
        }
        else {
            if (preg_match('/<div class="content-center fl">(.*?)<div class="content-left fl">/s', FetchDataString($link), $document)) {
                $document = $document[0];
            }
            else {
                echo "Error code: 1 - Lỗi khi lấy tất cả link";
                return;
            }

            $document = str_replace('href="/gl/', 'href="http://vnexpress.net/gl/', $document);
			 $document = str_replace('href="/GL/', 'href="http://vnexpress.net/gl/', $document);
			 
			
            if (!preg_match_all('%href="(http://.*?/)"%', $document, $result, PREG_PATTERN_ORDER))
                return false;

            $n = sizeof($result[1]);

            for ($i = 0; $i < $n; $i++) {
                $flag = 0;
                for ($j = $i + 1; $j < $n; $j++)
                    if ($result[1][$j] == $result[1][$i])
                        $flag = 1;

                if ($flag == 0)
                    array_push($this->links, $result[1][$i]);
            }
        }

        return true;
    }

    function GetInfo($link) {
        $this->Init();

        if (preg_match('/<div class="content">(.*)<div( class="box-item")? style="margin-top:5px;margin-bottom:5px;">/s', FetchDataString($link), $regs)) {
            $document = $regs[1];
        }
        else {
            echo "Error code: 2 - Lỗi khi bóc tách chi tiết " . $link;
            return;
        }

        $search = array('@new SWFObject\("/Library@si',
            '@"/Service/FlashVideo/@si',
            '%playlistfile=/Service/FlashVideo%s',
            '@<script([^>]*?)src="(.*?)">(.*?)</script>@si',
            '@<IMG([^>]*?)src="(.*?)"(.*?)>@si',
            '%<a.*?href="(/.*?)".*?>%i',
            '%<p id=pShow>.*?</p>%',
            '%<div id="dComment" class="dtopitem_all">.*?</script>%s');

        $replace = array('new SWFObject("http://vnexpress.net/Library',
            '"http://vnexpress.net/Service/FlashVideo/',
            'playlistfile=http://vnexpress.net/Service/FlashVideo',
            '<script \1 src="http://vnexpress.net\2">\3</script>',
            '<img\1src="http://vnexpress.net\2"\3>',
            '<a href="http://vnexpress.net\1" target="_blank">',
            '',
            '');

       
        $document = preg_replace($search, $replace, $document);

        if (!preg_match('/<img.*?src="(.*(?:jpg|gif|png|bmp))".*?>/i', $document, $regs))
            $this->imgurl = "";
        else
            $this->imgurl = $regs[1];

        if (!preg_match('%<H1 class=Title>(.*)</H1>%s', $document, $regs))
            return false;

        $this->title = strip_tags($regs[1]);

        if (!preg_match('%<H2 class=Lead>(.*)</H2>%s', $document, $regs))
            return false;

        $this->intro = strip_tags(preg_replace('%>.<a .*>.*?</a>%i', '', $regs[1]));

        $this->content = substr($document, strpos($document, $regs[0]) + strlen($regs[0]));

        $filter = array('<div class="tag-parent">','<div style="margin-top:5px;margin-bottom:5px;">', '<div class="box-item" style="margin-top:5px;margin-bottom:5px;">', '<TABLE cellSpacing=0 cellPadding=0 width=100% border=0><tr><td id=tdTopic');

        for ($i = 0; $i < sizeof($filter); $i++) {
            $pos = strrpos($this->content, $filter[$i]);
            if ($pos)
                $this->content = substr($this->content, 0, $pos);
        }

        $this->content = trim($this->content);



        if (trim($this->imgurl) != "" && !preg_match('%<img .*? />%i', $this->content)) {
            $this->content = sprintf("<br /><center><img border=\"1\" alt=\"\" src=\"%s\" style=\"margin:5px;\"></center><br />", trim($this->imgurl)) . $this->content;
        }

        if (substr($this->content, -6) == "</div>")
            $this->content = trim(substr($this->content, 0, strlen($this->content) - 6));

        $this->content = lowertags($this->content);
        $this->content = preg_replace(array('/ style="text-align: justify;/i', '/ class="?Normal"?/i', '/class="Image"/i', '/ align=right/i'), array('', '', 'style="text-align:center;font-size:10pt;font-style:italic;"', ' style="text-align: right;"'), $this->content);

        return true;
    }

}

?>